<template>
  <batchUpdateValueDialog
      :dialogInfo="batchUpdateValueDialogInfo"
      @baseMounted="baseMounted"
      :self="self">
    </batchUpdateValueDialog>
    <setTablesDialog
      :dialogInfo="setTablesDialogInfo"
      @baseMounted="baseMounted"
      :self="self">
    </setTablesDialog>
</template>

<script>
import { ref, unref, watch, onMounted } from 'vue'
import { post } from "/src/api/index";
import batchUpdateValueDialog from '../edit_dialog.vue'
import setTablesDialog from '../edit_dialog.vue'
import { ElMessage } from '/src/utils'

export default {
    emits: ['baseMounted'],
    components:{
      batchUpdateValueDialog,
      setTablesDialog,
    },
    props: {
      self:{},
    },
    setup(prop,context) {

      let setTablesDialogInfo = ref({
        title: '设置表及别名：【表注释（表名）】 - 【是否生成代码】 - 【别名】',
        visible:false,
        loading: false,
        labelWidth: '400px',
        rules:[],
        form:{},
        columns:[
        ],
        onconfirm: (param) => {
          let {dialogInfo, selectRows} = param
          let tables = ''
          let form = dialogInfo.form
          dialogInfo.columns.forEach( column => {
            if (form[column.prop+'_active']) {
              let alias = form[column.prop+'_alias']
              if (alias) {
                alias = alias.trim()
              }
              if (tables) {
                tables += ','
              }
              if (alias && alias != column.prop) {
                tables += column.prop + ' as ' + alias
              } else {
                tables += column.prop
              }
            }
          })
          let count = 0;
          selectRows.forEach( item => {
            post('sdp_project', 'update', {id:item.id, tables:tables})
            .then( data => {
              count++;
              if (count == selectRows.length) {
                ElMessage.success('更新成功')
                prop.self.handleSearch();
                dialogInfo.hide(param)
                dialogInfo.loading = false;
              }
            })
            .catch( err => {
              ElMessage.error('更新失败')
              dialogInfo.loading = false;
            })
          })
        },
        hide: (param) => {
          const {dialogInfo} = param
          dialogInfo.form = {}
          dialogInfo.visible = false
          dialogInfo.loading = false
        },
        show: (param) => {
          let {dialogInfo, query, selectRows} = param

          selectRows = unref(selectRows)
          if (!selectRows || !selectRows.length) {
            ElMessage.error('请选择至少一条记录进行操作')
            return;
          }

          dialogInfo.columns.forEach( item => {
            if (item.oninit) {
              item.oninit({...unref(prop.self), mode: 'init', dialogInfo:dialogInfo, column:item, columns: dialogInfo.columns, form: dialogInfo.form})
            }
          })

          dialogInfo.visible = true
        },
      })
      prop.self.setTablesDialogInfo = setTablesDialogInfo
      
        const baseMounted = (v) => {
        }

        return {
          batchUpdateValueDialogInfo: prop.self.batchUpdateValueDialogInfo,
          setTablesDialogInfo,
          baseMounted,
        };
    },
};
</script>

<style scoped>

</style>
